package com.example.ythhome.login.api;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.ythhome.login.entity.Menu;
import com.example.ythhome.login.entity.ResponseData;
import com.example.ythhome.login.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("api/menu")
public class MenuController {

    @Autowired
    MenuService menuService;


    @GetMapping("/query")
    @ResponseBody
    public ResponseData<List> query() {
        return new ResponseData<List>(ResponseData.StatusCodeEnum_SUCCESS, menuService.list());
    }

    @GetMapping("/queryByUserId")
    @ResponseBody
    public ResponseData<List> queryByUserId() {
        Integer userId = StpUtil.getLoginIdAsInt();
        return new ResponseData<List>(ResponseData.StatusCodeEnum_SUCCESS, menuService.queryMenuByUserId(userId));
    }

    @PostMapping("/add")
    @ResponseBody
    public ResponseData<Boolean> add(@RequestBody Menu menu) {
        return new ResponseData<Boolean>(ResponseData.StatusCodeEnum_SUCCESS, menuService.save(menu));
    }

    @PostMapping("/edit")
    @ResponseBody
    public ResponseData<Boolean> query(@RequestBody Menu menu) {
        UpdateWrapper<Menu> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("menu_id",menu.getMenuId());
        return new ResponseData<Boolean>(ResponseData.StatusCodeEnum_SUCCESS, menuService.update(menu,updateWrapper));
    }

    @PostMapping("/delete")
    @ResponseBody
    public ResponseData<Boolean> delete(@RequestBody Integer id) {
        UpdateWrapper<Menu> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("menu_id",id);
        UpdateWrapper<Menu> updateWrapper1 = new UpdateWrapper<>();
        updateWrapper1.eq("parent_id",id);
        menuService.remove(updateWrapper1);
        return new ResponseData<Boolean>(ResponseData.StatusCodeEnum_SUCCESS, menuService.remove(updateWrapper));
    }
}
